import os
import shutil

def copy_files_by_extension(source_dir, destination_dir, extension):
    # 创建目标文件夹
    if not os.path.exists(destination_dir):
        os.makedirs(destination_dir)

    # 遍历源目录
    for root, dirs, files in os.walk(source_dir):
        for file in files:
            if file.endswith(extension):
                source_path = os.path.join(root, file)
                destination_path = os.path.join(destination_dir, file)

                # 拷贝文件
                shutil.copy2(source_path, destination_path)
                print(f"Copied: {file} from {source_path} to {destination_path}")

# 指定源目录、目标目录和要查找的文件扩展名
source_directory = r'F:\github\test'
destination_directory = r'F:\github\test2'
file_extension = '.py'  # 你可以修改为其他扩展名

# 调用函数进行拷贝
copy_files_by_extension(source_directory, destination_directory, file_extension)
